﻿using System;
using System.Web.UI;
using TierApplication;
using System.Data;
using System.Text.RegularExpressions;

public partial class ForeProgram_ProductView : System.Web.UI.Page
{

    #region Page_Load
    /// <summary>
    /// Page_Load
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (this.Request.QueryString["Productid"] != null)
            {
                txtProductid.Text = this.Request.QueryString["Productid"].ToString();
            }
            T_ACCOUNT account = new T_ACCOUNT();
            lbl400Hotline.Text = account.GET_400HOTLINE();
            LoadData();
        }
        if (GetProductCount(txtProductid.Text) <= 0)
        {
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), DateTime.Now.ToString("yyyy-MM-dd"), "<script>Init('1');</script>");
        }
        else
        {
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), DateTime.Now.ToString("yyyy-MM-dd"), "<script>Init('');</script>");
        }
    }
    #endregion

    #region 加载数据
    /// <summary>
    /// 加载数据
    /// </summary>
    private void LoadData()
    {
        T_PRODUCT logic = new T_PRODUCT();
        DataSet ds = logic.GetList(" productid='" + txtProductid.Text + "'");
        if(ds!=null && ds.Tables.Count!=0 && ds.Tables[0].Rows.Count!=0)
        {
            lblProductname.Text = ds.Tables[0].Rows[0]["productname"].ToString();
            lblMarketprice.Text = ds.Tables[0].Rows[0]["marketprice"].ToString();
            lblDiscountprice.Text = ds.Tables[0].Rows[0]["UNITPRICE"].ToString();
            lblSpread.Text = ds.Tables[0].Rows[0]["DISCOUNTPRICE"].ToString();
            lbldiscountVal.Text = "( " + ds.Tables[0].Rows[0]["DISCOUNT"].ToString();
            imgProduct.ImageUrl = "~/ProductImages/" + ds.Tables[0].Rows[0]["productimage"].ToString();
            imgProduct.ToolTip = lblProductname.Text;
            divContent.InnerHtml = ds.Tables[0].Rows[0]["descn"].ToString();
        }
    }
    #endregion

    #region 事件
    /// <summary>
    /// 事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbShop_Click(object sender, EventArgs e)
    {
        if (GetProductCount(txtProductid.Text) <= 0)
        {
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), DateTime.Now.ToString("yyyy-MM-dd"), "<script>alert('商品暂时缺货！');</script>");
            return;
        }
        bool bReturn = false;
        T_CART cart = new T_CART();
        cart.PRODUCTID = txtProductid.Text;
        cart.UNITPRICE = Convert.ToDecimal(lblDiscountprice.Text);
        DataSet ds = cart.GetList(" productid='" + txtProductid.Text + "' And accountid='" + Security.GetAccountId() + "'");
        if (ds != null && ds.Tables.Count != 0 && ds.Tables[0].Rows.Count != 0)
        {
            cart.QUANTITY = Convert.ToDecimal(txtQuantity.Text) + Convert.ToDecimal(ds.Tables[0].Rows[0]["QUANTITY"]);
            cart.CARTID = ds.Tables[0].Rows[0]["CARTID"].ToString();
            bReturn = cart.UpdateCartCount(cart);
        }
        else
        {
            cart.CARTID = cart.GetCartId();
            if (cart.CARTID.Equals("-1"))
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), DateTime.Now.ToLongTimeString(), "alert('跳转失败！');swithBtn('display');", true);
                return;
            }
            cart.QUANTITY = Convert.ToDecimal(txtQuantity.Text);
            cart.ACCOUNTID = Security.GetAccountId();
            cart.Add();
            bReturn = true;
        }
        if (bReturn)
        {
            Page.Response.Redirect("shopCartView.aspx");
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), DateTime.Now.ToLongTimeString(), "alert('跳转失败！');swithBtn('display');", true);
        }
    }
    protected void txtQuantity_TextChanged(object sender, EventArgs e)
    {

        bool check = CheckProduct(txtProductid.Text, txtQuantity.Text);
        if (!check)
            txtQuantity.Text = "1";
    }
    #endregion

    #region 检验商品数量
    /// <summary>
    /// 检验商品数量
    /// </summary>
    /// <param name="product"></param>
    /// <param name="sl"></param>
    /// <returns></returns>
    private bool CheckProduct(string product, string sl)
    {
        Regex rg = new Regex("^([0-9]*)$");
        if (!rg.Match(sl).Success)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), DateTime.Now.ToLongTimeString(), "alert('请输入正确的数字！');", true);
            return false;
        }
        int count = Convert.ToInt32(sl);
        if (count <= 0)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), DateTime.Now.ToLongTimeString(), "alert('数量不能小于1，请删除此货物！');", true);
            return false;
        }
        if (count >10)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), DateTime.Now.ToLongTimeString(), "alert('修改数量失败！');", true);
            return false;
        }
        if (count > GetProductCount(product))
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), DateTime.Now.ToLongTimeString(), "alert('库存不足，请重新填写数量！');", true);
            return false;
        }
        return true;
    }
    #endregion

    #region 取得商品数量
    /// <summary>
    /// 取得商品数量
    /// </summary>
    /// <param name="productid"></param>
    /// <returns></returns>
    private int GetProductCount(string productid)
    {
        string strWhere = " And productid='" + productid + "'";
        strWhere += " And productitemstatus='1'";
        T_PRODUCTITEM ent = new T_PRODUCTITEM();
        return ent.GetProductCount(strWhere);
    }
    #endregion

    #region 数量
    /// <summary>
    /// 数量
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void imgSub_Click(object sender, ImageClickEventArgs e)
    {
        bool check = CheckProduct(txtProductid.Text, (Convert.ToInt32(txtQuantity.Text) - 1).ToString());
        if (check)
        {
            txtQuantity.Text = (Convert.ToInt32(txtQuantity.Text) - 1).ToString();
        }
        else
        {
            txtQuantity.Text = "1";
        }
    }
    protected void imgAdd_Click(object sender, ImageClickEventArgs e)
    {
        bool check = CheckProduct(txtProductid.Text, (Convert.ToInt32(txtQuantity.Text) + 1).ToString());
        if (check)
        {
            txtQuantity.Text = (Convert.ToInt32(txtQuantity.Text) + 1).ToString();
        }
        else
        {
            txtQuantity.Text = "1";
        }
    }
    #endregion
}